#include "http.h"

httprequestp parseHTTP(int socket, char *inputHTTP) {

	char cmdHTTP[HTTP_PARSER_ARRAYSIZE] = "";
	char filenameHTTP[HTTP_PARSER_ARRAYSIZE] = "";
	char protocolHTTP[HTTP_PARSER_ARRAYSIZE] = "";
	char *ifModP, ifMod[] = "If-Modified-Since:";
	httprequestp result;
	int nLine, ok = sscanf(inputHTTP,"%s %s %s",cmdHTTP,filenameHTTP,protocolHTTP);
	result = (httprequestp) malloc(sizeof(httprequest));
	strcpy(result->filename, "");
	strcpy(result->date, "");

	printf("%s\n", inputHTTP);


	if (strcmp(cmdHTTP, "GET")==0) {

		if ( ifModP = strstr( inputHTTP, ifMod ) ) {
			ifModP += strlen(ifMod)+1;
			memcpy(result->date, ifModP, (nLine = (strchr(ifModP, '\n') - ifModP)));
			result->date[nLine-1] = '\0';
			result->http_command = CMD_GET_CONDITIONAL;
			strcpy(result->filename, filenameHTTP);
			printf("I got a conditional GET with the date: %s\n",result->date);
			
			return result;
		} else {
			result->http_command = CMD_GET;
			strcpy(result->filename, filenameHTTP);

			printf("Non-conditional GET");

			return result;
		}
	} else {

		result->http_command = CMD_UNSUPPORTED;

		if ( !strcmp(filenameHTTP, "") )			
			strcpy(result->filename, "No file");	
		else
			strcpy(result->filename, filenameHTTP);	

		printf("Not a supported command");

		return result;
	}
}

void responseWrapper(SOCKET socket, httprequestp request){
	
	char fntmp[256];
	httpresponsep response;
	FILE *ftmp;

	response = (httpresponsep) malloc(sizeof(httpresponse));
	
	if (strcmp(request->filename, "/") == 0){
		strcpy(request->filename, "index.html");
	} else {
		strcpy(request->filename, (request->filename)+1);
	}
	
	strcpy(fntmp, "");
	strcpy(fntmp,getHTTPFolder());
	strcat(fntmp,request->filename);
	strcpy(request->filename, fntmp);	
	
	if ( (ftmp = fopen(fntmp,"r")) == NULL ) {
		strcpy(response->code, "404");
		strcpy(response->msg, "not found");
	} else if (!isModifiedSince(request->filename, request->date)) {
		strcpy(response->code, "304");
		mimeType(response, request);	
		strcpy(response->msg, "Not modified");
	} else {
		fclose(ftmp);
		strcpy(response->code, "200");
		strcpy(response->msg, "Ok");
		mimeType(response, request);	
	}

	sendData(socket, request, response); 
	
	printf("\n%s\n",request->filename);
	
	free(response);
}

void mimeType(httpresponsep response, httprequestp request) {
	
	int i;
	char *types[] = { "html", "jpg", "jpeg", "gif", "png" }, *MIME[] = { "text/html", "image/jpeg", "image/jpeg", "image/gif", "image/png", "text/plain" }, *type;
	type = strrchr(request->filename, '.');
	if (type == NULL) {
		strcpy(response->mime, MIME[5]);	
		return;
	}
	type++;
	for ( i = 0; i < 4; i++) 
		if ( !strcmp( type, types[i] )){
			strcpy(response->mime, MIME[i]);			
			return;			
		}
	strcpy(response->mime, MIME[5]);	
	return;
}

void httpHead(char *msg, httpresponsep response, int msgLen, char* filename) {
	char len[10];
	char lastModified[64];
	char curTime[64];
	time_t rawtime;
	struct stat buf;
	itoa (msgLen,len,10);

	strcpy(msg, "HTTP/1.1 ");
	if (strcmp(response->code, "404")){
		stat(filename, &buf);
		strftime(lastModified, 100, "%a, %d %b %Y %H:%M:%S GMT", localtime(&buf.st_mtime));
	}

	time(&rawtime);
	strftime(curTime, 100, "%a, %d %b %Y %H:%M:%S GMT", localtime(&rawtime));

	strcat(msg, response->code);

	if(!strcmp(response->code, "200")){
		strcat(msg, " OK");
	}
	if(!strcmp(response->code, "304")){
		strcat(msg, " Not Modified");
		printf("\nH E A D E R:\n%s\n", msg);
	}
	if(!strcmp(response->code, "404")){		
		strcat(msg, " Not Found");
		printf("\nH E A D E R:\n%s\n", msg);
	}
	strcat(msg, "\nDate: ");
	strcat(msg, curTime);
	strcat(msg, "\nServer: drone 1.23");
	strcat(msg, "\nContent-Length: ");
	strcat(msg, len);
	strcat(msg, "\nConnection: close");
	if (strcmp(response->code, "404")){
		strcat(msg, "\nLast-Modified: ");
		strcat(msg, lastModified);
		strcat(msg, "\nContent-Type: ");
		strcat(msg, response->mime);
	}
	strcat(msg, "\n\n");
	printf("\nH E A D E R:\n%s", len);
	printf("\nH E A D E R:\n%s", msg);

}